package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.search.service.ApUserSearchService;
import com.heima.search.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
@Slf4j
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper,ApUserSearch> implements ApUserSearchService {
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        //检查参数

        if(dto==null||dto.getSearchWords()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry behaviorEntry = behaviorFeign.findByUserIdOrEntryId(user.getId(), dto.getEquipmentId());
        LambdaQueryWrapper<ApUserSearch> queryWrapper = Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, behaviorEntry.getEntryId())
                .eq(ApUserSearch::getStatus,1).orderByDesc(ApUserSearch::getCreatedTime);
        Page<ApUserSearch> page =new Page<>(0,5);
        IPage<ApUserSearch> apUserSearchIPage = this.page(page, queryWrapper);


        return ResponseResult.okResult(apUserSearchIPage.getRecords());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        //检查参数
        if (dto==null||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserSearch apUserSearch = this.getById(dto.getId());
        if (apUserSearch!=null){
            apUserSearch.setStatus(0);
           this.updateById(apUserSearch);
        }
        return ResponseResult.okResult();
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {
        ApUserSearch userSearch = getOne(
                Wrappers.<ApUserSearch>lambdaQuery()
                        .eq(ApUserSearch::getEntryId, entryId)
                        .eq(ApUserSearch::getKeyword, searchWords)
        );

        if(userSearch!=null){
            userSearch.setStatus(1);
            userSearch.setCreatedTime(new Date());
            updateById(userSearch);
        }else {

            userSearch = new ApUserSearch();
            userSearch.setEntryId(entryId);
            userSearch.setKeyword(searchWords);
            userSearch.setStatus(1);
            userSearch.setCreatedTime(new Date());
            save(userSearch);
        }

    }
}
